﻿using System;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using LTJ.Entity;
using LTJ.Common;
using LTJ.Config;
using LTJ.FrameWork;

namespace LTJ.WebPage.UserManage.loginfo
{
    /// <summary>
    /// 规则操作日志
    /// </summary>
    public partial class ruleloggrid : UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataGrid();
            }
        }

        protected void BindDataGrid()
        {
            string strWhere = "cid = " + companyinfo.id;

            if (ViewState["condition"] != null && ViewState["condition"].ToString() != "")
            {
                strWhere += " AND " + ViewState["condition"].ToString();
            }

            DataGrid1.VirtualItemCount = OpLogs.GetAllRuleLogCount(strWhere);
            DataGrid1.DataSource = OpLogs.GetRuleLogsByPage(DataGrid1.CurrentPageIndex + 1, DataGrid1.PageSize, strWhere, "", "");
            DataGrid1.DataBind();
        }

        protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            DataGrid1.CurrentPageIndex = e.NewPageIndex;
            BindDataGrid();
        }

        protected void GoToPagerButton_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        /// <summary>
        /// 搜索点击
        /// </summary>
        protected void check_Click(object sender, EventArgs e)
        {
            string conditionstr = SASRequest.GetString("lname");
            if (conditionstr != "" && Utils.IsSafeSqlString(conditionstr))
            {
                ViewState["condition"] = "lname like '%" + conditionstr + "%'";
            }
            else
            {
                ViewState["condition"] = "";
            }

            string timestr = "";
            if (!string.IsNullOrEmpty(starttime.SelectedDate.ToString()) && !string.IsNullOrEmpty(endtime.SelectedDate.ToString()))
            {
                timestr = "createdate between '" + starttime.SelectedDate.ToString() + "' AND '" + endtime.SelectedDate.ToString() + "'";
            }
            else if (!string.IsNullOrEmpty(starttime.SelectedDate.ToString()))
            {
                timestr = "createdate >= '" + starttime.SelectedDate.ToString() + "'";
            }
            else if (!string.IsNullOrEmpty(endtime.SelectedDate.ToString()))
            {
                timestr = "createdate <= '" + endtime.SelectedDate.ToString() + "'";
            }

            if (ViewState["condition"] != null && ViewState["condition"].ToString() != "")
            {
                if (timestr != "") ViewState["condition"] = ViewState["condition"].ToString() + " AND " + timestr;
            }
            else ViewState["condition"] = timestr;

            DataGrid1.CurrentPageIndex = 0;
            BindDataGrid();
        }

        /// <summary>
        /// 重新绑定
        /// </summary>
        protected void reclick_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        /// <summary>
        /// 批量删除日志
        /// </summary>
        protected void del_Click(object sender, EventArgs e)
        {
            if (SASRequest.GetString("cid") != "")
            {
                OpLogs.DelRuleLogs(SASRequest.GetString("cid"));
                BindDataGrid();
            }
        }

        #region Web 窗体设计器生成的代码

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            DataGrid1.PageIndexChanged += new DataGridPageChangedEventHandler(DataGrid1_PageIndexChanged);
            DataGrid1.GoToPagerButton.Click += new EventHandler(GoToPagerButton_Click);
            DataGrid1.AllowCustomPaging = true;
            DataGrid1.TableHeaderName = "规则操作日志列表";
            DataGrid1.Attributes.Add("borderStyle", "0");
            DataGrid1.DataKeyField = "id";
            DataGrid1.ColumnSpan = 12;
            DataGrid1.SaveDSViewState = true;

            check.Click += new EventHandler(check_Click);
            reclick.ServerClick += new EventHandler(reclick_Click);
            //del.Click += new EventHandler(del_Click);
        }

        #endregion
    }
}